package com.jiepos.mpos.pay;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.biz.customer.service.AppCustomerInfoService;
import com.jiepos.mpos.common.constant.RspCodeConstant;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.AppPayDao;
import com.jiepos.mpos.common.entity.App_Custom_InfEntity;
import com.jiepos.mpos.common.entity.GpsEntity;
import com.jiepos.mpos.common.entity.PayEntity;
import com.jiepos.mpos.common.entity.PayResultEntity;
import com.jiepos.mpos.common.service.AbrBaseHttpService;
import com.jiepos.mpos.core.util.HttpClientUtils;
import com.jiepos.mpos.core.util.MathUtils;
import com.jiepos.mpos.core.util.StringUtils;
import com.jiepos.mpos.inter.fourelement.FourElements;
import com.jiepos.mpos.inter.fourelement.service.IFourElementService;
import com.jiepos.mpos.inter.intopieces.dto.RequestDTO;
import com.jiepos.mpos.inter.intopieces.dto.ResponseDTO;
import com.jiepos.mpos.inter.intopieces.utils.SignatureUtil;
import com.jiepos.mpos.pay.quickpay.service.IQuickPayService;

/**
 * 
 * <p>标题: —— 支付业务主服务类，负责公用业务逻辑的处理，路由的选择，
 * 限额、风控的控制，判断调用何种接口,标准化数据后调用各业务接口，调用完接口后标准化数据返回此服务类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @author 娄伟峰
 * @version 1.0
 * @since 2017年9月8日 下午4:17:36
 * @history（历次修订内容、修订人、修订时间等）
 */
@Service
public class PayService extends AbrBaseHttpService {
	public static final Logger log = LoggerFactory.getLogger(PayService.class);

	@Autowired
	private MainBusinessService mainBusinessService;

	@Autowired
	private AppLogTransJnlsPayService appLogTransJnlsPayService;
	
	@Autowired
	private TransRouteService transRouteService;
	
	@Autowired
	private LogAbstractService logAbstractService;
	
	@Autowired
	private IFourElementService iFourElementService;
	
	@Autowired
	private AppCustomerInfoService customerInfoService;
	
	@Autowired
	private AppPayDao appPayDao;

	/**
	 * 根据路由相关业务逻辑判断获取对应服务类
	 * 
	 * @return IQuickPayService 接口服务
	 */
	private IQuickPayService getChannelService(Map dataMap) {
		// TODO 根据路由相关业务逻辑判断获取对应服务类
		return CommonService.getPayServices().get(dataMap.get("channel_No"));
	}
	/**
	 * 
	 * 接口: 预下单接口
     * @param customNo 客户编号
	 * @param traceNo  客户端流水号
	 * @param orderAmt 订单金额
	 * @param orderNo 订单号
	 * @param orderType  订单类型，用于区分是快捷支付订单、支付宝订单、微信订单
	 * @param cardNo 交易卡号
	 * @param service_uuid 业务uuid 
	 * @param service_param_uuid  业务参数uuid
	 * @param payEntity 订单实体对象，用于扩展订单参数
	 * @param payResMap 请求集合
	 * @return
	 * @since 2017年10月21日
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> preOrder(HttpServletRequest request, GpsEntity gpsEntity, String customNo,
			String orderAmt, String random_number, String service_no, String param_no, String snd_dt, String snd_ts,
			String pay_mode, String login_id, String serviceData, PayEntity payEntity) {
		Map<String, String> payReqMap = new HashMap<String, String>(36);// 请求标准化报文
		Map<String, String> payRspMap = null;// 响应报文
		try {
			// 检查必填项
			String[] values = { service_no, param_no, customNo, snd_dt, snd_ts, orderAmt, login_id};
			if (!getCkeckMap(values)) {
				log.info("[{}]请求参数必传项{}",SystemConstant.PRE_ORDER_MSG, Arrays.toString(values));
				return getRspMap(RspCodeConstant.RSPCODE_300014, null, null);
			}
			// 验签
			Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), gpsEntity.getSign(),
					random_number, gpsEntity.getVersion(), serviceData, login_id, request.getServletPath(), gpsEntity,
					SystemConstant.PRE_ORDER_MSG);
			// 验签不通过
			if (!SystemConstant.RSPCODE_SUCCESS.equals(checkSignMap.get(SystemConstant.RSP_CODE))) {
				return checkSignMap;
			}
			if (!checkService(service_no, param_no, payReqMap)) {
				return getRspMap(payReqMap.get(SystemConstant.RSP_CODE), null, null);
			}
			
			// 组装标准请求报文
			payReqMap.put("custom_no", customNo);// 客户编号
			payReqMap.put("trans_at", orderAmt);// 交易金额
			payReqMap.put("trans_id", SystemConstant.PRE_ORDER_CODE);// 交易码
			payReqMap.put("trans_nm", SystemConstant.PRE_ORDER_MSG);// 交易名称
			payReqMap.put("snd_dt", snd_dt);// 客户端上送日期
			payReqMap.put("snd_ts", snd_ts);// 客户端上送时间
			payReqMap.put("pay_mode", pay_mode == null ? "" : pay_mode);// 支付方式
			payReqMap.put("login_id", login_id);
			payReqMap.put("serial_number", gpsEntity.getSerial_number());// APP流水号

			// 检查会员信息是否存在(判断会员等级)
			if (!mainBusinessService.checkCustomerInfo(payReqMap)) {
				String desc = String.format(String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300020)),payReqMap.get(SystemConstant.RSP_MSG));
				return getRspMap(RspCodeConstant.RSPCODE_300020, desc, null);
			}

			// 合法性校验通过，记录交易流水，并生成支付系统唯一流水号，用于提交到支付通道,流水表交易结果为XX-处理中
			if (!appLogTransJnlsPayService.save(payReqMap, gpsEntity)
					&& !StringUtils.isEmpty(payReqMap.get(SystemConstant.RSP_CODE))) {
				return getRspMap(payReqMap.get(SystemConstant.RSP_CODE), null, null);
			}

			// 会员状态合法性检查
			if (!mainBusinessService.checkCustomerState(payReqMap)
					&& !StringUtils.isEmpty(payReqMap.get(SystemConstant.RSP_CODE))) {
				appLogTransJnlsPayService.update(payReqMap, payReqMap.get(SystemConstant.RSP_CODE).substring(4, 6),
						String.valueOf(getRspCodeListMap().get(payReqMap.get(SystemConstant.RSP_CODE))));
				return getRspMap(payReqMap.get(SystemConstant.RSP_CODE), null, null);
			}

			// 机构状态合法性检查
			if (!mainBusinessService.checkInsState(payReqMap)
					&& !StringUtils.isEmpty(payReqMap.get(SystemConstant.RSP_CODE))) {
				appLogTransJnlsPayService.update(payReqMap, payReqMap.get(SystemConstant.RSP_CODE).substring(4, 6),
						String.valueOf(getRspCodeListMap().get(payReqMap.get(SystemConstant.RSP_CODE))));
				return getRspMap(payReqMap.get(SystemConstant.RSP_CODE), null, null);
			}
			log.info("预下单成功异步通知管理系统 loc_trace_no=" + payReqMap.get("loc_trace_no"));
			mainBusinessService.asyncNotifyMpos(payReqMap.get("loc_trace_no"));
			// 组客户端返回报文
			payRspMap = new HashMap<String, String>();
			payRspMap.put("service_no", service_no);
			payRspMap.put("param_no", param_no);
			payRspMap.put("custom_no", customNo);
			payRspMap.put("serial_number", gpsEntity.getSerial_number());
			payRspMap.put("snd_dt", snd_dt);
			payRspMap.put("snd_ts", snd_ts);
			payRspMap.put("order_no", payReqMap.get("order_no"));
			payRspMap.put("loc_trace_no", payReqMap.get("loc_trace_no"));// 本地流水号
			payRspMap.put("version", gpsEntity.getVersion());
			payRspMap.put("appType", gpsEntity.getAppType());
			payRspMap.put("app_id", gpsEntity.getApp_id());
			return getRspMap(SystemConstant.RSPCODE_SUCCESS, null, payRspMap);
		} catch (Exception e) {
			log.error("预下单交易处理发生异常!", e);
			return getRspMap(SystemConstant.RSPCODE_ERROR, null, payRspMap);
		}
	}
	
	/**
	 * 
	 * 接口: 快捷支付订单
	 * @param customNo 客户编号
	 * @param traceNo  客户端流水号
	 * @param orderAmt 订单金额
	 * @param orderNo 订单号
	 * @param orderType  订单类型，用于区分是快捷支付订单、支付宝订单、微信订单
	 * @param cardNo 交易卡号
	 * @param service_uuid 业务uuid 
	 * @param service_param_uuid  业务参数uuid
	 * @param payEntity 订单实体对象，用于扩展订单参数
	 * @param payResMap 请求集合
	 * @return
	 * @since 2017年10月21日
	 */
	public Map payOrder(HttpServletRequest request, GpsEntity gpsEntity, String random_number, String service_no,
			String param_no, String custom_no, String snd_dt, String snd_ts, String order_no, String act_out_no,
			String crd_exp, String phone, String cvv2, String rand, String pin, String trace_no, String orderType,
			String login_id, String bankcard_uuid, String serviceData, PayEntity payEntity) {
		Map<String, String> payReqMap = new HashMap<String, String>();// 请求标准化报文
		Map<String, String> payRspMap = null;// 响应报文
		try {
			// 检查必填项
			String[] values = { service_no, param_no, custom_no, snd_dt, snd_ts, order_no, act_out_no, trace_no,
					orderType, login_id, gpsEntity.getLocaladdress() };
			if (!getCkeckMap(values)) {
				log.info("[{}]请求参数必传项{}",SystemConstant.PAY_ORDER_MSG, Arrays.toString(values));
				return getRspMap(RspCodeConstant.RSPCODE_300014, null, null);
			}
			// 验签
			Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), gpsEntity.getSign(),
					random_number, gpsEntity.getVersion(), serviceData, login_id, request.getServletPath(), gpsEntity,
					SystemConstant.PAY_ORDER_MSG);
			// 验签不通过
			if (!SystemConstant.RSPCODE_SUCCESS.equals(checkSignMap.get(SystemConstant.RSP_CODE))) {
				return checkSignMap;
			}
			if (!checkService(service_no, param_no, payReqMap)) {
				return getRspMap(payReqMap.get(SystemConstant.RSP_CODE), null, null);
			}
			// 组装标准化处理报文
			payReqMap.put("custom_no", custom_no);// 客户编号
			payReqMap.put("snd_trace_no", trace_no);// 客户流水
			payReqMap.put("order_no", order_no);// 订单号
			payReqMap.put("act_out_no", act_out_no);// 支付账号(终端上送)
			payReqMap.put("trans_id", SystemConstant.PAY_ORDER_CODE);// 交易码
			payReqMap.put("trans_nm", SystemConstant.PAY_ORDER_MSG);// 交易名称
			payReqMap.put("random_number", random_number);// 随机数
			payReqMap.put("service_no", service_no);// 业务编号
			payReqMap.put("param_no", param_no);// 业务参数编号
			payReqMap.put("snd_dt", snd_dt);
			payReqMap.put("snd_ts", snd_ts);
			payReqMap.put("act_out_no", act_out_no);
			payReqMap.put("crd_exp", crd_exp);
			payReqMap.put("phone", phone);
			payReqMap.put("cvv2", cvv2);
			payReqMap.put("rand", rand);
			payReqMap.put("pin", pin);
			payReqMap.put("trace_no", trace_no);
			payReqMap.put("orderType", orderType);
			payReqMap.put("login_id", login_id);
			payReqMap.put("bankcard_uuid", bankcard_uuid);
			payReqMap.put("payUrl",
					request.getRequestURL().substring(0, request.getRequestURL().indexOf(request.getServletPath())));
			payReqMap.put("serial_number", gpsEntity.getSerial_number());//APP流水号

			// VIP充值
			if (SystemConstant.SERVICE_NO_100002.equals(service_no)) {
				payReqMap.put("trans_id", SystemConstant.PAY_RECHARGE_CODE);// 交易码
				payReqMap.put("trans_nm", SystemConstant.PAY_RECHARGE_MSG);// 交易名称
			}

			// 检查会员信息是否存在(判断会员等级)
			if (!mainBusinessService.checkCustomerInfo(payReqMap)) {
				String desc = String.format(String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300020)),payReqMap.get(SystemConstant.RSP_MSG));
				return getRspMap(RspCodeConstant.RSPCODE_300020, desc, null);
			}
			
			// 查询预下单订单本地流水号(获取本地流水号及交易金额)
			if (!appLogTransJnlsPayService.getLocTraceNo(payReqMap)) {
				String rspCode = RspCodeConstant.RSPCODE_300012;
				if (!StringUtils.isEmpty(payReqMap.get(SystemConstant.RSP_CODE))) {
					rspCode = payReqMap.get(SystemConstant.RSP_CODE);
				}
				return getRspMap(rspCode, null, null);
			}
			
			if (!StringUtils.isEmpty(bankcard_uuid)) {
				String actOutNo = mainBusinessService.getActOutNo(payReqMap);
				if (!StringUtils.isEmpty(actOutNo)) {
					act_out_no = actOutNo;
				} else {
					return getRspMap(RspCodeConstant.RSPCODE_300025, null, null);
				}
			} else {
				String name = payReqMap.get("name");
				String phoneNo = payReqMap.get("phone");
				String certificate_no = payReqMap.get("certificate_no");
				Map<String, String> paramMap = new HashMap<String, String>();
				paramMap.put(SystemConstant.SESSION_LOGIN_USER, login_id);
				/*查询会员所属app机构，获取机构uuid*/
				App_Custom_InfEntity customer = customerInfoService.getCustomByNo(custom_no);
				Map<String, Object> resultMap = iFourElementService.queryFourElement(new FourElements(customer.getIns_uuid(), name, phoneNo, certificate_no, act_out_no), paramMap);
				if (!SystemConstant.RSPCODE_SUCCESS.equals(resultMap.get(SystemConstant.RSP_CODE))) {
					appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300028.substring(4, 6),
							String.valueOf(resultMap.get(SystemConstant.RSP_MSG)));
					return getRspMap(RspCodeConstant.RSPCODE_300028, null, null);
				}
			}

			// 更新流水(交易码、交易名称、受卡方所在地日期、受卡方所在地时间、受卡方系统跟踪号)
			if (!appLogTransJnlsPayService.update(payReqMap,gpsEntity)) {
				return getRspMap(payReqMap.get(SystemConstant.RSP_CODE), null, null);
			}
			
			// 根据卡号查询卡bin及卡性质(获取卡bin信息)
			if (!mainBusinessService.getCardBinAttrInfo(payReqMap) && !StringUtils.isEmpty(payReqMap.get(SystemConstant.RSP_CODE))) {
				appLogTransJnlsPayService.update(payReqMap, payReqMap.get(SystemConstant.RSP_CODE).substring(4, 6),
						String.valueOf(getRspCodeListMap().get(payReqMap.get(SystemConstant.RSP_CODE))));
				return getRspMap(payReqMap.get(SystemConstant.RSP_CODE), null, null);
			}

			// 快捷支付客户端第一次上送的信息(服务端解密并保存)
			if (StringUtils.isEmpty(bankcard_uuid)) {
				if (!mainBusinessService.saveCardInfo(payReqMap,gpsEntity.getVersion()) && !StringUtils.isEmpty(payReqMap.get(SystemConstant.RSP_CODE))) {
					appLogTransJnlsPayService.update(payReqMap, payReqMap.get(SystemConstant.RSP_CODE).substring(4, 6),
							String.valueOf(getRspCodeListMap().get(payReqMap.get(SystemConstant.RSP_CODE))));
					return getRspMap(payReqMap.get(SystemConstant.RSP_CODE), null, null);
				}
			}
			// 根据会员编号查询收款账号信息(收款账户及开户行)
			if (!mainBusinessService.getActInCardInfo(payReqMap)) {
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300054.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300054)));
				return getRspMap(RspCodeConstant.RSPCODE_300054, null, null);
			}
			// 会员状态合法性检查
			if (!mainBusinessService.checkCustomerState(payReqMap)
					&& !StringUtils.isEmpty(payReqMap.get(SystemConstant.RSP_CODE))) {
				appLogTransJnlsPayService.update(payReqMap, payReqMap.get(SystemConstant.RSP_CODE).substring(4, 6),
						String.valueOf(getRspCodeListMap().get(payReqMap.get(SystemConstant.RSP_CODE))));
				return getRspMap(payReqMap.get(SystemConstant.RSP_CODE), null, null);
			}
			
			// 机构状态合法性检查
			if (!mainBusinessService.checkInsState(payReqMap)
					&& !StringUtils.isEmpty(payReqMap.get(SystemConstant.RSP_CODE))) {
				appLogTransJnlsPayService.update(payReqMap, payReqMap.get(SystemConstant.RSP_CODE).substring(4, 6),
						String.valueOf(getRspCodeListMap().get(payReqMap.get(SystemConstant.RSP_CODE))));
				return getRspMap(payReqMap.get(SystemConstant.RSP_CODE), null, null);
			}		

			// 会员、机构限额合法性校验，校验不通过直接更新流水状态
			if (!mainBusinessService.validateParamLimit(payReqMap)) {
				String desc = String.format(String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300007)),payReqMap.get(SystemConstant.RSP_MSG));
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300007.substring(4, 6),
						desc);
				return getRspMap(RspCodeConstant.RSPCODE_300007, desc, null);
			}

			// 交易受理类型
			if (!mainBusinessService.checkTranType(payReqMap)) {
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300008.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300008)));
				return getRspMap(RspCodeConstant.RSPCODE_300008, null, null);
			}
			// TODO 根据订单相关参数获取交易路由服务，判断最终走哪些通道分组。
			log.info(
					"路由调用的参数[login_id={},act_out_no={},custom_no={},custom_level={},ins_uuid={},area_uuid={},service_uuid={},"
					+ "service_param_uuid={},card_ins_cd={},crd_nature={},trans_at={},service_ins_uuid={},card_bin={}]",
					login_id, payReqMap.get("act_out_no"), payReqMap.get("custom_no"), payReqMap.get("custom_level"),
					payReqMap.get("ins_uuid"), payReqMap.get("area_uuid"), payReqMap.get("service_uuid"),
					payReqMap.get("service_param_uuid"), payReqMap.get("card_ins_cd"), payReqMap.get("crd_nature"),
					Double.valueOf(payReqMap.get("trans_at")), payReqMap.get("service_ins_uuid"),
					payReqMap.get("card_bin"),payReqMap.get("loc_trace_no"));
			Map<String, String> routeMap = transRouteService.getTransRouteInfo(login_id, payReqMap.get("act_out_no"),
					payReqMap.get("custom_no"), payReqMap.get("custom_level"), payReqMap.get("ins_uuid"),
					payReqMap.get("area_uuid"), payReqMap.get("service_uuid"), payReqMap.get("service_param_uuid"),
					payReqMap.get("card_ins_cd"), payReqMap.get("crd_nature"),
					Double.valueOf(payReqMap.get("trans_at")), payReqMap.get("service_ins_uuid"),
					payReqMap.get("card_bin"),payReqMap.get("loc_trace_no"));
			IQuickPayService payService = getChannelService(routeMap);// 获取渠道服务		
			if ("1".equals(routeMap.get("resultNum"))) {
				if (payService == null) {
					log.error("[{}]交易流水路由返回的渠道编号无对应的渠道服务,路由信息=[{}]！", payReqMap.get("loc_trace_no"), routeMap);
					appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300033.substring(4, 6),
							String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300033)));
					return getRspMap(RspCodeConstant.RSPCODE_300033, null, null);
				}
				if (!appLogTransJnlsPayService.update(payReqMap, routeMap)) {
					log.error("[{}]交易流水更新发生异常[routeMap={},payReqMap={}]！", payReqMap.get("loc_trace_no"), routeMap,
							payReqMap);
					return getRspMap(SystemConstant.RSPCODE_ERROR, null, null);
				}
			} else {
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300026.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300026)));
				return getRspMap(RspCodeConstant.RSPCODE_300026, null, null);
			}

			Map<String, String> dataMap = CommonService.getStandardData(payReqMap);// 报文标准化处理
			log.info("系统请求报文转标准化渠道请求报文:[{}]",dataMap);
			Map rspMap = payService.payOrder(routeMap.get("trf_mchnt_cd"), order_no, payReqMap.get("trans_at"),
					payReqMap.get("phone"), payReqMap.get("certNo"), payReqMap.get("acct_nm"),
					payReqMap.get("act_out_no"), null, dataMap);
			// 渠道异常
			if (StringUtils.isEmpty(rspMap)) {
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300035.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300035)));
				return getRspMap(RspCodeConstant.RSPCODE_300035, null, null);
			}
			String rspCode = CommonService.encapResultCode(String.valueOf(rspMap.get("rspcode")));
			if (!SystemConstant.RSPCODE_SUCCESS.equals(rspCode)) {
				String rspMsg = null;
				if (!StringUtils.isEmpty(rspMap.get("rspmsg"))) {
					rspMsg = String.valueOf(rspMap.get("rspmsg"));
				}
				appLogTransJnlsPayService.update(payReqMap, String.valueOf(rspMap.get("rspcode")), rspMsg);
				return getRspMap(rspCode, rspMsg, null);
			} else {
				log.info("支付成功异步通知管理系统 loc_trace_no=" + payReqMap.get("loc_trace_no"));
				mainBusinessService.asyncNotifyMpos(payReqMap.get("loc_trace_no"));
				appLogTransJnlsPayService.updateTraceNo(payReqMap.get("loc_trace_no"),String.valueOf(rspMap.get("loc_dt")),String.valueOf(rspMap.get("loc_ts")));
				payRspMap = new HashMap<String, String>();
				payRspMap.put("service_no", service_no);
				payRspMap.put("param_no", param_no);
				payRspMap.put("custom_no", payReqMap.get("custom_no"));
				payRspMap.put("serial_number", payReqMap.get("serial_number"));
				payRspMap.put("snd_dt", payReqMap.get("snd_dt"));
				payRspMap.put("snd_ts", payReqMap.get("snd_ts"));
				payRspMap.put("order_no", payReqMap.get("order_no"));// 订单号
				payRspMap.put("trans_at", payReqMap.get("trans_at"));// 交易金额
				payRspMap.put("loc_trace_no", payReqMap.get("loc_trace_no"));// 本地流水号
				payRspMap.put("version", gpsEntity.getVersion());
				payRspMap.put("appType", gpsEntity.getAppType());
				payRspMap.put("app_id", gpsEntity.getApp_id());
				payRspMap.put("trf_channel_id", payReqMap.get("channel_No"));//渠道编号
				payRspMap.put("phone", payReqMap.get("phone"));
				payRspMap.put("bankcard_uuid", payReqMap.get("bankcard_uuid"));//新增卡后多返回bankcard_uuid
				payRspMap.put("act_out_no", StringUtils.hideData("3",payReqMap.get("act_out_no")));
				payRspMap.put("channel_type", String.valueOf(rspMap.get("channel_type")));
				payRspMap.put("verify_code_type", String.valueOf(rspMap.get("verify_code_type")));
				payRspMap.put("action", String.valueOf(rspMap.get("action")));
				payRspMap.put("data", String.valueOf(rspMap.get("data")));
				payRspMap.put("url", String.valueOf(rspMap.get("url")));// 暂时未使用
				payRspMap.put("sourceData", String.valueOf(rspMap.get("sourceData")));
				return getRspMap(rspCode, null, payRspMap);
			}
		} catch (Exception e) {
			log.error("快捷支付服务发生异常", e);
			return getRspMap(SystemConstant.RSPCODE_ERROR, null, payRspMap);
		}
	}
	
	/**
	 * 
	 * 接口: 手机验证码接口
	 * @param request
	 * @param response
	 * @param gpsEntity
	 * @param random_number
	 * @param custom_no
	 * @param loc_trace_no
	 * @param phone
	 * @param verify_code
	 * @param pay_pass_word
	 * @param login_id
	 * @param serviceData
	 * @return
	 * @since 2017年12月14日
	 */
	public Map verifyCode(HttpServletRequest request, HttpServletResponse response, GpsEntity gpsEntity,
			String random_number, String custom_no, String loc_trace_no, String phone, String verify_code,
			String pay_pass_word, String login_id, String serviceData) {
		Map<String, String> payReqMap = new HashMap<String, String>();// 请求标准化报文
		Map<String, String> payRspMap = null;// 响应报文
		try {
			// 检查必填项
			String[] values = { custom_no, loc_trace_no, phone, verify_code, pay_pass_word, login_id};
			if (!getCkeckMap(values)) {
				log.info("[{}]请求参数必传项{}", "快捷支付手机验证码", Arrays.toString(values));
				return getRspMap(RspCodeConstant.RSPCODE_300014, null, null);
			}
			// 验签
			Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), gpsEntity.getSign(),
					random_number, gpsEntity.getVersion(), serviceData, login_id, request.getServletPath(), gpsEntity,
					"快捷支付手机验证码");
			// 验签不通过
			if (!SystemConstant.RSPCODE_SUCCESS.equals(checkSignMap.get(SystemConstant.RSP_CODE))) {
				return checkSignMap;
			}
			payReqMap.put("random_number", random_number);// 随机数
			payReqMap.put("custom_no", custom_no);// 客户编号
			payReqMap.put("loc_trace_no", loc_trace_no);// 流水号
			payReqMap.put("phone", phone);// 银行预留手机号
			payReqMap.put("verify_code", verify_code);// 验证码密文
			payReqMap.put("pay_pass_word", pay_pass_word);// 支付密码密文

			// 检查会员信息是否存在(判断会员等级)
			if (!mainBusinessService.checkCustomerInfo(payReqMap)) {
				String desc = String.format(String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300020)),payReqMap.get(SystemConstant.RSP_MSG));
				return getRspMap(RspCodeConstant.RSPCODE_300020, desc, null);
			}

//			// 解密验证码及支付密码
			if (!mainBusinessService.verifyData(payReqMap, gpsEntity.getVersion(), pay_pass_word)
					&& !StringUtils.isEmpty(payReqMap.get(SystemConstant.RSP_CODE))) {
				appLogTransJnlsPayService.update(payReqMap, payReqMap.get(SystemConstant.RSP_CODE).substring(4, 6),
						String.valueOf(getRspCodeListMap().get(payReqMap.get(SystemConstant.RSP_CODE))));
				return getRspMap(payReqMap.get(SystemConstant.RSP_CODE), null, null);
			}
			//校验支付密码
			if (StringUtils.isEmpty(payReqMap.get("pay_pass_word"))
					|| !payReqMap.get("pay_pass_word").equals(payReqMap.get("decrypt_pay_pass_word"))) {
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300060.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300060)));
				return getRspMap(RspCodeConstant.RSPCODE_300060, null, null);
			}
	
			Map<String, String> tranInfo = appLogTransJnlsPayService.getTranInfo(loc_trace_no);
			payReqMap.put("loc_dt", tranInfo.get("loc_dt"));
			payReqMap.put("loc_ts", tranInfo.get("loc_ts"));
			payReqMap.put("trf_mchnt_cd", tranInfo.get("trf_mchnt_cd"));
			payReqMap.put("act_out_no", tranInfo.get("act_out_no"));
			payReqMap.put("trans_at", tranInfo.get("trans_at"));
			payReqMap.put("channel_No", tranInfo.get("trf_channel_id"));
			mainBusinessService.getActOutNo(payReqMap);
			Map<String, String> dataMap = CommonService.getStandardData(payReqMap);// 报文标准化处理
			IQuickPayService payService = CommonService.getPayServices().get(tranInfo.get("trf_channel_id"));
			if (StringUtils.isEmpty(payService)) {
				log.error("[{}]交易流水路由返回的渠道编号无对应的[{}]渠道服务", payReqMap.get("loc_trace_no"), tranInfo.get("trf_channel_id"));
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300033.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300033)));
				return getRspMap(RspCodeConstant.RSPCODE_300033, null, null);
			}
			Map rspMap = payService.verifyCode(dataMap);
			// 渠道异常
			if (StringUtils.isEmpty(rspMap)) {
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300035.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300035)));
				return getRspMap(RspCodeConstant.RSPCODE_300035, null, null);
			}
			String rspCode = CommonService.encapResultCode(String.valueOf(rspMap.get("rspcode")));
			if (!SystemConstant.RSPCODE_SUCCESS.equals(rspCode)) {
				String rspMsg = null;
				if (!StringUtils.isEmpty(rspMap.get("rspmsg"))) {
					rspMsg = String.valueOf(rspMap.get("rspmsg"));
				}
				appLogTransJnlsPayService.update(payReqMap, String.valueOf(rspMap.get("rspcode")), rspMsg);
				return getRspMap(rspCode, rspMsg, null);
			} else {
				appLogTransJnlsPayService.updateTraceNo(payReqMap.get("loc_trace_no"),String.valueOf(rspMap.get("loc_dt")),String.valueOf(rspMap.get("loc_ts")));
				payRspMap = new HashMap<String, String>();
				payRspMap.put("custom_no", payReqMap.get("custom_no"));
				payRspMap.put("loc_trace_no", payReqMap.get("loc_trace_no"));// 本地流水号
				payRspMap.put("phone", payReqMap.get("phone"));
				payRspMap.put("verify_code", payReqMap.get("verifyCode"));
				payRspMap.put("pay_pass_word", pay_pass_word);
				payRspMap.put("version", gpsEntity.getVersion());
				payRspMap.put("appType", gpsEntity.getAppType());
				payRspMap.put("app_id", gpsEntity.getApp_id());
				return getRspMap(rspCode, null, payRspMap);
			}

		} catch (Exception e) {
			log.error("快捷支付手机验证码服务发生异常!", e);
			return getRspMap(SystemConstant.RSPCODE_ERROR, null, payRspMap);
		}
	}

	
	/**
	 * 订单查询
	 * @param customNo
	 *            客户编号
	 * @param traceNo
	 *            客户端流水号
	 * @param orderAmt
	 *            订单金额
	 * @param orderNo
	 *            订单号
	 * @param orderType
	 *            订单类型，用于区分是快捷支付订单、支付宝订单、微信订单
	 * @param cardNo
	 *            交易卡号
	 * @param service_uuid
	 *            业务uuid
	 * @param service_param_uuid
	 *            业务参数uuid
	 * @param payEntity
	 *            订单实体对象，用于扩展订单参数
	 * @param paramMap
	 * 			  参数map
	 * @return
	 */
	public Map queryResult(HttpServletRequest request,GpsEntity gpsEntity,String random_number,String service_no,
			String param_no,String custom_no,String snd_dt,String snd_ts,String order_no,String trace_no,String loc_trace_no,String login_id, String serviceData, PayEntity payEntity){
		//log.info("订单查询order_no=" + order_no + "请求");
		Map<String,String> payReqMap = new HashMap<String,String>();		
		try {			
			// 检查必填项
			String[] values = { service_no, param_no, custom_no, snd_dt, snd_ts, order_no, trace_no,login_id };
			
			if (!getCkeckMap(values)) {
				log.info("订单查询请求参数必传项{}" ,Arrays.toString(values));
				return getRspMap(RspCodeConstant.RSPCODE_300014, null, null);
			}
			// 请求数据合法性校验						
			Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), gpsEntity.getSign(), random_number,
	                gpsEntity.getVersion(), serviceData, login_id, "httppay/queryResult.do", gpsEntity, "订单查询接口");
			// 验签不通过
			if (!SystemConstant.RSPCODE_SUCCESS.equals(checkSignMap.get(SystemConstant.RSP_CODE))) {
				return checkSignMap;
			}
			
			if (!checkService(service_no, param_no, payReqMap)) {
				return getRspMap(payReqMap.get(SystemConstant.RSP_CODE), null, null);
			}
			
			// 组装标准化处理报文
			payReqMap.put("trans_id", SystemConstant.PAY_ORDER_QUERY_CODE);// 交易码
			payReqMap.put("trans_nm", SystemConstant.PAY_ORDER_QUERY_MSG);// 交易名称
			payReqMap.put("random_number", random_number);// 随机数
			payReqMap.put("service_no", service_no);// 业务编号
			payReqMap.put("param_no", param_no);// 业务参数编号
			payReqMap.put("custom_no", custom_no);// 会员编号
			payReqMap.put("snd_dt", snd_dt);// 受卡方所在地日期
			payReqMap.put("snd_ts", snd_ts);// 受卡方所在地时间
			payReqMap.put("order_no", order_no);// 订单号
			payReqMap.put("trace_no", trace_no);// 订单号
			payReqMap.put("loc_trace_no", loc_trace_no);// 本地流水号（支付系统）
			payReqMap.put("login_id", login_id);// App登录账号
			payReqMap.put("serial_number", gpsEntity.getSerial_number());// APP流水号

			// 检查会员信息是否存在(判断会员等级)
			if (!mainBusinessService.checkCustomerInfo(payReqMap)) {
				String desc = String.format(String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300020)),payReqMap.get(SystemConstant.RSP_MSG));
				return getRspMap(RspCodeConstant.RSPCODE_300020, desc, null);
			}

			// TODO 查询订单结果
			Map<String, String> transMap = appLogTransJnlsPayService.getTransJnlsByNo(payReqMap.get("order_no"),
					payReqMap.get("loc_trace_no"),payReqMap.get("trace_no"),null);

			if(transMap!=null&&!transMap.isEmpty()){
				if(!StringUtils.isBlank(transMap.get("trf_channel_id"))&&SystemConstant.RSPCODE_XX.equals(transMap.get("rspcode"))){															
					Map<String,String> map = new HashMap<String,String>();
					map.put("channel_No", transMap.get("trf_channel_id"));
					Map<String,String> queryMap = new HashMap<String,String>();
					queryMap.put("trf_mchnt_cd", transMap.get("trf_mchnt_cd"));
					queryMap.put("trf_term_id", transMap.get("trf_term_id"));
					queryMap.put("chnlOrderId", "");
					queryMap.put("loc_trace_no", transMap.get("loc_trace_no"));
					transMap=getChannelService(map).queryChannelOrder(queryMap);					
				}
			}else{
				return getRspMap(RspCodeConstant.RSPCODE_300012, null, null);
			}

			// TODO 重新组装标准化响应消息
			Map<String, String> dataMap=new HashMap<String,String>();
			dataMap.put("service_no", service_no);
			dataMap.put("param_no", param_no);
			dataMap.put("custom_no", custom_no);
			dataMap.put("serial_number", gpsEntity.getSerial_number());
			dataMap.put("snd_dt", snd_dt);
			dataMap.put("snd_ts", snd_ts);
			dataMap.put("order_no", order_no);
			dataMap.put("trans_at", String.valueOf(transMap.get("trans_at")));
			dataMap.put("loc_trace_no", transMap.get("loc_trace_no"));
			dataMap.put("complete_dt", transMap.get("complete_dt"));
			dataMap.put("complete_ts", transMap.get("complete_ts"));
			dataMap.put("midfee", String.valueOf(transMap.get("midfee")));
			dataMap.put("stl_amt", String.valueOf(transMap.get("stl_amt")));
			if(SystemConstant.AUDIT_STL_0.equals(transMap.get("stlst"))){
				dataMap.put("stlrt", "未清算");
			}else if(SystemConstant.AUDIT_STL_1.equals(transMap.get("stlst"))){
				dataMap.put("stlrt", "已清算");
			}			
			dataMap.put("rsprt", transMap.get("rspcode")+"-"+transMap.get("rspmsg"));
			dataMap.put("stl_dt", transMap.get("stl_dt"));
			dataMap.put("version", gpsEntity.getVersion());
			dataMap.put("appType", gpsEntity.getAppType());
			dataMap.put("app_id", gpsEntity.getApp_id());
			return getRspMap(SystemConstant.RSPCODE_SUCCESS, null, dataMap);
			
			
		} catch (Exception e) {
			log.error("订单查询服务发生异常",e);
			return getRspMap(SystemConstant.RSPCODE_ERROR, null, null);
		}		
	}
	
	
	/**
	 * 下发秘钥
	 * @param customNo
	 *            客户编号
	 * @param traceNo
	 *            客户端流水号
	 * @param orderAmt
	 *            订单金额
	 * @param orderNo
	 *            订单号
	 * @param orderType
	 *            订单类型，用于区分是快捷支付订单、支付宝订单、微信订单
	 * @param cardNo
	 *            交易卡号
	 * @param service_uuid
	 *            业务uuid
	 * @param service_param_uuid
	 *            业务参数uuid
	 * @param payEntity
	 *            订单实体对象，用于扩展订单参数
	 * @param paramMap
	 * 			  参数map
	 * @return
	 */
	public Map paramKey(HttpServletRequest request,GpsEntity gpsEntity,String random_number,
			String custom_no,String snd_dt,String snd_ts,String custom_source,String client_version,
			String login_id, String serviceData, PayEntity payEntity){
		//log.info("下发秘钥custom_no=" + custom_no + "请求");	
		Map<String,String> payReqMap = new HashMap<String,String>();
		try {
			// 检查必填项
			String[] values = { custom_no, snd_dt, snd_ts, custom_source, client_version,login_id };			
			if (!getCkeckMap(values)) {
				log.info("下发秘钥请求参数必传项{}" ,Arrays.toString(values));
				return getRspMap(RspCodeConstant.RSPCODE_300014, null, null);
			}
						
			// 请求数据合法性校验			
			Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), gpsEntity.getSign(), random_number,
	                gpsEntity.getVersion(), serviceData, login_id, "httppay/getParamKey.do", gpsEntity, "下发秘钥接口");
			
			// 验签不通过
			if (!SystemConstant.RSPCODE_SUCCESS.equals(checkSignMap.get(SystemConstant.RSP_CODE))) {
				return checkSignMap;
			}
			
			// 组装标准化处理报文
			payReqMap.put("trans_id", SystemConstant.PAY_PARAMKEY_CODE);// 交易码
			payReqMap.put("trans_nm", SystemConstant.PAY_PARAMKEY_MSG);// 交易名称
			payReqMap.put("random_number", random_number);// 随机数		
			payReqMap.put("custom_no", custom_no);// 会员编号
			payReqMap.put("snd_dt", snd_dt);// 下发日期
			payReqMap.put("snd_ts", snd_ts);// 下发时间
			payReqMap.put("custom_source",custom_source);// 客户来源
			payReqMap.put("client_version", client_version);// 客户端版本
			payReqMap.put("login_id", login_id);// 		App登录账号	
			payReqMap.put("serial_number", gpsEntity.getSerial_number());//APP流水号
			
			// 检查会员信息是否存在(判断会员等级)
			if (!mainBusinessService.checkCustomerInfo(payReqMap)) {
				String desc = String.format(String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300020)),payReqMap.get(SystemConstant.RSP_MSG));
				return getRspMap(RspCodeConstant.RSPCODE_300020, desc, null);
			}			
						
			// 新增客户端随机数及token记录
			
			Map result=mainBusinessService.saveClientKey(payReqMap,gpsEntity);
			if(result!=null&&!result.isEmpty()){
				return result;
			}
			

			// TODO 重新组装标准化响应消息
			Map<String, String> dataMap = new HashMap<String, String>();
			dataMap.put("custom_no", custom_no);
			dataMap.put("serial_number", gpsEntity.getSerial_number());
			dataMap.put("snd_dt", snd_dt);
			dataMap.put("snd_ts", snd_ts);
			dataMap.put("pin_key", payReqMap.get("pin_key"));
			dataMap.put("cvv2_key", payReqMap.get("cvv2_key"));
			dataMap.put("rand_key", payReqMap.get("rand_key"));
			dataMap.put("exp_key", payReqMap.get("exp_key"));
			dataMap.put("version", gpsEntity.getVersion());
			dataMap.put("appType", gpsEntity.getAppType());
			dataMap.put("app_id", gpsEntity.getApp_id());
			return getRspMap(SystemConstant.RSPCODE_SUCCESS, null,
					dataMap);
			
		} catch (Exception e) {
			log.error("下发秘钥服务发生异常",e);
			return getRspMap(SystemConstant.RSPCODE_ERROR, null, null);
		}
	}
	
	/**
	 * 提现
	  * @param customNo 客户编号
	 * @param traceNo  客户端流水号
	 * @param orderAmt 订单金额
	 * @param orderNo 订单号
	 * @param orderType  订单类型，用于区分是快捷支付订单、支付宝订单、微信订单
	 * @param cardNo 交易卡号
	 * @param service_uuid 业务uuid 
	 * @param service_param_uuid  业务参数uuid
	 * @param payEntity 订单实体对象，用于扩展订单参数
	 * @param payResMap 请求集合
	 * @return
	 * @return 返回给客户端
	 */
	public Map withdraw(HttpServletRequest request,GpsEntity gpsEntity,String random_number,String service_no,
			String param_no,String custom_no,String snd_dt,String snd_ts,String trans_at,String acc_type,
			String pay_password,String rand,String login_id,String cash_fee, String serviceData, PayEntity payEntity) {
		//log.info("接受到提现custom_no=" + custom_no + "请求");
		Map<String, String> payReqMap = new HashMap<String, String>();
		try {
			// 检查必填项
			String[] values = { service_no,param_no,custom_no, snd_dt, snd_ts, trans_at, acc_type,cash_fee,login_id };			
			if (!getCkeckMap(values)) {
				log.info("提现请求参数必传项{}" ,Arrays.toString(values));
				return getRspMap(RspCodeConstant.RSPCODE_300014, null, null);
			}
			
			// 请求数据合法性校验			
			Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), gpsEntity.getSign(), random_number,
	                gpsEntity.getVersion(), serviceData, login_id, "httppay/withdraw.do", gpsEntity, "提现接口");
			
			// 验签不通过
			if (!SystemConstant.RSPCODE_SUCCESS.equals(checkSignMap.get(SystemConstant.RSP_CODE))) {
				return checkSignMap;
			}
			if (!checkService(service_no, param_no, payReqMap)) {
				return getRspMap(payReqMap.get(SystemConstant.RSP_CODE), null, null);
			}
			
			// 组装标准化处理报文
			payReqMap.put("trans_id", SystemConstant.PAY_WITHDRAL_CODE);// 交易码
			payReqMap.put("trans_nm", SystemConstant.PAY_WITHDRAL_MSG);// 交易名称
			payReqMap.put("random_number", random_number);// 随机数	
			payReqMap.put("service_no", service_no);// 业务编号
			payReqMap.put("param_no", param_no);// 业务参数编号
			payReqMap.put("custom_no", custom_no);// 会员编号
			payReqMap.put("snd_dt", snd_dt);// 提现日期
			payReqMap.put("snd_ts", snd_ts);// 提现时间
			payReqMap.put("trans_at", trans_at);// 交易金额
			payReqMap.put("acc_type",acc_type);// 账户类型
			payReqMap.put("pay_password", pay_password);// 支付密码
			payReqMap.put("rand", rand);//验证码
			payReqMap.put("login_id", login_id);//App登录账号
			payReqMap.put("cash_fee", cash_fee);//手续费（分）
			payReqMap.put("serial_number", gpsEntity.getSerial_number());//APP流水号
			
			// 检查会员信息是否存在(判断会员等级)
			// 检查会员信息是否存在(判断会员等级)
			if (!mainBusinessService.checkCustomerInfo(payReqMap)) {
				String desc = String.format(String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300020)),payReqMap.get(SystemConstant.RSP_MSG));
				return getRspMap(RspCodeConstant.RSPCODE_300020, desc, null);
			}								
			// 合法性校验通过，记录交易流水，并生成申请单号,流水表交易结果为XX-处理中
			Map map = logAbstractService.saveLogAbstract(payReqMap);
			if (map != null&&!map.isEmpty()) {
				return map;
			}
			
			// 会员状态合法性检查
			if (!mainBusinessService.checkCustomerState(payReqMap)
					&& !StringUtils.isEmpty(payReqMap.get(SystemConstant.RSP_CODE))) {
				logAbstractService.updateRspCd(payReqMap, payReqMap.get(SystemConstant.RSP_CODE),String.valueOf(getRspCodeListMap().get(payReqMap.get(SystemConstant.RSP_CODE))));
				return getRspMap(payReqMap.get(SystemConstant.RSP_CODE), null, null);
			}
			
			// 机构状态合法性检查
			if (!mainBusinessService.checkInsState(payReqMap)
					&& !StringUtils.isEmpty(payReqMap.get(SystemConstant.RSP_CODE))) {
				logAbstractService.updateRspCd(payReqMap, payReqMap.get(SystemConstant.RSP_CODE),String.valueOf(getRspCodeListMap().get(payReqMap.get(SystemConstant.RSP_CODE))));
				return getRspMap(payReqMap.get(SystemConstant.RSP_CODE), null, null);
			}
			
			// 校验会员账户状态
			map = mainBusinessService.checkAccountSt(payReqMap);
			if (map != null&&!map.isEmpty()) {
				logAbstractService.updateRspCd(payReqMap, RspCodeConstant.RSPCODE_300015,String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300015)));
				return map;
			}
			
			//校验单笔限额，当日次数，当日限额，当月次数，当月限额，提现日期和时间限制
			map=mainBusinessService.checkWithdraw(payReqMap);
			if(!SystemConstant.RSPCODE_SUCCESS.equals(map.get("rsp_code"))){
				logAbstractService.updateRspCd(payReqMap, String.valueOf(map.get("rsp_code")),String.format(String.valueOf(getRspCodeListMap().get(map.get("rsp_code"))),map.get("rsp_msg")));
				return getRspMap(String.valueOf(map.get("rsp_code")), String.format(String.valueOf(getRspCodeListMap().get(map.get("rsp_code"))),map.get("rsp_msg")), null);
			}
			
			//校验可用余额
			boolean result=mainBusinessService.checkAvailableAmt(payReqMap);
			if(!result){
				logAbstractService.updateRspCd(payReqMap, RspCodeConstant.RSPCODE_300022,String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300022)));
				return getRspMap(RspCodeConstant.RSPCODE_300022, null, null);
			}
												
			//客户提现申请变更客户余额和增加明细记录
			String rsp_code =logAbstractService.updateAbstract(payReqMap.get("apply_no"));
			
			//更新提现流水状态
			logAbstractService.updateRspCd(payReqMap, rsp_code, String.valueOf(getRspCodeListMap().get(rsp_code)));											

			// TODO 重新组装标准化响应消息
			Map<String, String> dataMap = new HashMap<String, String>();
			dataMap.put("service_no", service_no);
			dataMap.put("param_no", param_no);
			dataMap.put("custom_no", custom_no);
			dataMap.put("serial_number", gpsEntity.getSerial_number());
			dataMap.put("snd_dt", snd_dt);
			dataMap.put("snd_ts", snd_ts);
			dataMap.put("trans_at", trans_at);
			dataMap.put("order_no", payReqMap.get("apply_no"));
			dataMap.put("acc_type", acc_type);			
			dataMap.put("cash_fee", cash_fee);				
			dataMap.put("rsprt", rsp_code+"-"+String.valueOf(getRspCodeListMap().get(rsp_code)));	
			dataMap.put("version", gpsEntity.getVersion());
			dataMap.put("appType", gpsEntity.getAppType());
			dataMap.put("app_id", gpsEntity.getApp_id());
			return getRspMap(rsp_code, String.valueOf(getRspCodeListMap().get(rsp_code)),dataMap);			
			
		} catch (Exception e) {
			log.error("提现服务发生异常",e);
			return getRspMap(SystemConstant.RSPCODE_ERROR, null, null);
		}

	}
	
	/**
	 * 提现主动查询
	 * @param apply_no
	 * @return
	 * @throws ConnectTimeoutException
	 * @throws SocketTimeoutException
	 * @throws Exception
	 */
	public Map<String, String> withdrawQuery(String apply_no){
		log.info("接受到提现主动查询apply_no=" + apply_no + "请求");
		Map<String, String> payRspMap = new HashMap<String, String>();			
		try {
			//加签
			String sign="";
			//组装请求参数
			List<org.apache.http.NameValuePair> uwsData = new ArrayList<org.apache.http.NameValuePair>();
			uwsData.add(new BasicNameValuePair("sign", sign));
			uwsData.add(new BasicNameValuePair("order_no", apply_no));
			//发起请求 查询			
			try {
				String rspData = HttpClientUtils.post("https://218.94.61.124:8091/uws/ws/payrs/payimpl",
						uwsData, "html/text", 10000, 10000);
				if(!StringUtils.isBlank(rspData)){					
					Map<String,String> map=CommonService.parseJsonToMap(rspData);
					Map<String,String> dataMap=CommonService.parseJsonToMap(map.get("dataMap"));
					String stl_st=dataMap.get("stl_st");
					String trade_dt=dataMap.get("trade_dt");
					String trade_tm=dataMap.get("trade_tm");
					
					payRspMap.put("rsp_code", String.valueOf(map.get("rspCode")));							
					payRspMap.put("audit_stlflag", CommonService.convertStlst(stl_st));
					payRspMap.put("stl_amt", String.valueOf(MathUtils.div(Double.parseDouble(dataMap.get("stl_amt")), Double.parseDouble("100"), 2)));
					payRspMap.put("audit_dt", trade_dt);
					payRspMap.put("audit_ts", trade_tm);
				}				
			} catch (ConnectTimeoutException e) {				
				log.error("提现主动查询发生异常");	
				payRspMap.put("rsp_code", "99");
			} catch (SocketTimeoutException e) {				
				log.error("提现主动查询发生异常");	
				payRspMap.put("rsp_code", "99");
			} catch (Exception e) {				
				log.error("提现主动查询发生异常");	
				payRspMap.put("rsp_code", "99");
			}				
		} catch (Exception e) {
			log.error("提现主动查询发生异常");
			payRspMap.put("rsp_code", "99");
		}
		return payRspMap;
	}
	

	/**
	 * 根据订单号查询支付结果
	 * 
	 * @param orderNo
	 *            订单号
	 * @return
	 */
	public Map queryOrder(String orderNo) {
		return null;
	}

	/**
	 * 支付结果异步通知结果,内部使用
	 * 
	 * @param
	 * @return
	 */
	private Map notifyResult(PayResultEntity payResultEntity) {

		return null;
	}

	/**
	 * 甬易异步通知结果
	 * 
	 * @return
	 */
	public Map yoyiNotifyResult() {
		// 将接受到的甬易数据标准化成标准数据
		this.notifyResult(null);
		return null;
	}
	
	/**
	 * 
	 * 接口: 获取检查必填项的Map
	 * @return
	 * @since 2017年10月23日
	 */
	private boolean getCkeckMap(String[] values) {
		if (values == null || values.length == 0) {
			return false;
		}
		for (int i = 0; i < values.length; i++) {
			if (StringUtils.isBlank(values[i])) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * 
	 * 接口: 检查系统业务及系统业务参数
	 * @param service_no
	 * @param param_no
	 * @param payReqMap
	 * @return
	 * @since 2017年11月28日
	 */
	private boolean checkService(String service_no,String param_no,Map<String, String> payReqMap){
		String service_uuid_result = mainBusinessService.getServiceUuid(service_no);
		if (StringUtils.isEmpty(service_uuid_result)) {
			payReqMap.put(SystemConstant.RSP_CODE, RspCodeConstant.RSPCODE_300024);
			return false;
		}
		if (SystemConstant.INS_ST_0.equals(service_uuid_result)) {
			payReqMap.put(SystemConstant.RSP_CODE, RspCodeConstant.RSPCODE_300041);
			return false;
		}
		String service_param_uuid_result = mainBusinessService.getServiceParamUuid(param_no, service_uuid_result);
		if (StringUtils.isEmpty(service_param_uuid_result)) {
			payReqMap.put(SystemConstant.RSP_CODE, RspCodeConstant.RSPCODE_300040);
			return false;
		}
		if (SystemConstant.INS_ST_0.equals(service_param_uuid_result)) {
			payReqMap.put(SystemConstant.RSP_CODE, RspCodeConstant.RSPCODE_300042);
			return false;
		}
		payReqMap.put("service_uuid", service_uuid_result);// 业务主键
		payReqMap.put("service_param_uuid", service_param_uuid_result);// 业务参数主键
		return true;
	}
	
	/**
	 * 
	 * 接口: 发送短信验证码
	 * @param request
	 * @param response
	 * @param gpsEntity
	 * @param random_number
	 * @param custom_no
	 * @param loc_trace_no
	 * @param phone
	 * @param login_id
	 * @param serviceData
	 * @return
	 * @since 2017年12月14日
	 */
	public Map sendVerifyCode(HttpServletRequest request, HttpServletResponse response, GpsEntity gpsEntity,
			String random_number, String custom_no, String loc_trace_no, String phone, String login_id, String serviceData) {
		Map<String, String> payReqMap = new HashMap<String, String>();// 请求标准化报文
		Map<String, String> payRspMap = null;// 响应报文
		try {
			// 检查必填项
			String[] values = { custom_no, loc_trace_no, phone, login_id};
			if (!getCkeckMap(values)) {
				log.info("[{}]请求参数必传项{}", "快捷支付手机验证码", Arrays.toString(values));
				return getRspMap(RspCodeConstant.RSPCODE_300014, null, null);
			}
			// 验签
//			Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), gpsEntity.getSign(),
//					random_number, gpsEntity.getVersion(), serviceData, login_id, request.getServletPath(), gpsEntity,
//					"快捷支付手机验证码");
//			// 验签不通过
//			if (!SystemConstant.RSPCODE_SUCCESS.equals(checkSignMap.get(SystemConstant.RSP_CODE))) {
//				return checkSignMap;
//			}
			payReqMap.put("random_number", random_number);// 随机数
			payReqMap.put("custom_no", custom_no);// 客户编号
			payReqMap.put("loc_trace_no", loc_trace_no);// 流水号
			payReqMap.put("phone", phone);// 银行预留手机号

			// 检查会员信息是否存在(判断会员等级)
			if (!mainBusinessService.checkCustomerInfo(payReqMap)) {
				String desc = String.format(String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300020)),payReqMap.get(SystemConstant.RSP_MSG));
				return getRspMap(RspCodeConstant.RSPCODE_300020, desc, null);
			}
	
			Map<String, String> tranInfo = appLogTransJnlsPayService.getTranInfo(loc_trace_no);
			payReqMap.put("loc_dt", tranInfo.get("loc_dt"));
			payReqMap.put("loc_ts", tranInfo.get("loc_ts"));
			payReqMap.put("trf_mchnt_cd", tranInfo.get("trf_mchnt_cd"));
			payReqMap.put("act_out_no", tranInfo.get("act_out_no"));
			mainBusinessService.getActOutNo(payReqMap);
			Map<String, String> dataMap = CommonService.getStandardData(payReqMap);// 报文标准化处理
			IQuickPayService payService = CommonService.getPayServices().get(tranInfo.get("trf_channel_id"));
			if (StringUtils.isEmpty(payService)) {
				log.error("[{}]交易流水路由返回的渠道编号无对应的[{}]渠道服务", payReqMap.get("loc_trace_no"), tranInfo.get("trf_channel_id"));
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300033.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300033)));
				return getRspMap(RspCodeConstant.RSPCODE_300033, null, null);
			}
			Map rspMap = payService.sendVerifyCode(dataMap);
			// 渠道异常
			if (StringUtils.isEmpty(rspMap)) {
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300035.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300035)));
				return getRspMap(RspCodeConstant.RSPCODE_300035, null, null);
			}
			String rspCode = CommonService.encapResultCode(String.valueOf(rspMap.get("rspcode")));
			if (!SystemConstant.RSPCODE_SUCCESS.equals(rspCode)) {
				String rspMsg = null;
				if (!StringUtils.isEmpty(rspMap.get("rspmsg"))) {
					rspMsg = String.valueOf(rspMap.get("rspmsg"));
				}
				appLogTransJnlsPayService.update(payReqMap, String.valueOf(rspMap.get("rspcode")), rspMsg);
				return getRspMap(rspCode, rspMsg, null);
			} else {
				appLogTransJnlsPayService.updateTraceNo(payReqMap.get("loc_trace_no"),String.valueOf(rspMap.get("loc_dt")),String.valueOf(rspMap.get("loc_ts")));
				payRspMap = new HashMap<String, String>();
				payRspMap.put("custom_no", payReqMap.get("custom_no"));
				payRspMap.put("loc_trace_no", payReqMap.get("loc_trace_no"));// 本地流水号
				payRspMap.put("phone", payReqMap.get("phone"));
				payRspMap.put("version", gpsEntity.getVersion());
				payRspMap.put("appType", gpsEntity.getAppType());
				payRspMap.put("app_id", gpsEntity.getApp_id());
				return getRspMap(rspCode, null, payRspMap);
			}

		} catch (Exception e) {
			log.error("快捷支付手机验证码服务发生异常!", e);
			return getRspMap(SystemConstant.RSPCODE_ERROR, null, payRspMap);
		}
	}
	
	/**
	 * 
	 * 接口: 外接支付接口
	 * @param request 请求对象
	 * @param requestDto  请求实体
	 * @param bizContent 业务对象
	 * @param response 响应对象
	 * @return
	 */
	public Map payGateService(HttpServletRequest request, RequestDTO requestDto, Map<String, String> bizContent, ResponseDTO response) {
		Map<String, String> payReqMap = new HashMap<String, String>();// 请求标准化报文
		Map<String, String> payRspMap = new HashMap<String, String>();// 响应报文
		
		//String version = requestDto.getVersion();
		String instCode = requestDto.getInstCode();
		String trace_no = requestDto.getRequestId();
		String service_no = bizContent.get("service_no");
		String param_no = bizContent.get("param_no"); 
		String order_no = bizContent.get("order_no"); 
		String custom_no = bizContent.get("mchnt_cd"); 
		String term_id = bizContent.get("term_id"); 
		String order_time = bizContent.get("order_time"); 
		String snd_dt = order_time.substring(0,8); 
		String snd_ts = order_time.substring(8,14); 
		String trans_at = bizContent.get("trans_at");
		String act_out_no = bizContent.get("act_out_no"); 
		String ext_filed = bizContent.get("ext_filed"); 
		String crd_exp = bizContent.get("crd_exp"); 
		String phone = bizContent.get("phone"); 
		String cvv2 = bizContent.get("cvv2"); 
		String rand = bizContent.get("rand"); 
		String orderType = bizContent.get("order_type");
		String bg_url = bizContent.get("bg_url"); 
		String page_url = bizContent.get("page_url"); 
		String login_id = custom_no; 
		
		// 组装标准化处理报文
		payReqMap.put("inte_flag", "gate");// 客户编号
		//payReqMap.put("version", version);// 客户编号
		payReqMap.put("instCode", instCode);// 客户编号
		payReqMap.put("trace_no", trace_no);// 客户编号
		payReqMap.put("service_no", service_no);// 业务编号
		payReqMap.put("param_no", param_no);// 业务参数编号
		payReqMap.put("order_no", order_no);// 订单号
		payReqMap.put("custom_no", custom_no);// 客户编号
		payReqMap.put("term_id", term_id);// 客户编号
		payReqMap.put("snd_dt", snd_dt);
		payReqMap.put("snd_ts", snd_ts);
		payReqMap.put("trans_at", trans_at);
		payReqMap.put("act_out_no", act_out_no);// 支付账号(终端上送)
		payReqMap.put("ext_filed", ext_filed);
		payReqMap.put("crd_exp", crd_exp);
		payReqMap.put("phone", phone);
		payReqMap.put("cvv2", cvv2);
		payReqMap.put("rand", rand);
		payReqMap.put("orderType", orderType);
		payReqMap.put("bg_url", bg_url);
		payReqMap.put("page_url", page_url);
		payReqMap.put("snd_trace_no", trace_no);// 客户流水
		payReqMap.put("trans_id", SystemConstant.PAY_ORDER_CODE);// 交易码
		payReqMap.put("trans_nm", SystemConstant.PAY_ORDER_MSG);// 交易名称
		payReqMap.put("payUrl",
				request.getRequestURL().substring(0, request.getRequestURL().indexOf(request.getServletPath())));
		payReqMap.put("serial_number", trace_no);//APP流水号
		payReqMap.put("login_id", custom_no);
		
		if (!checkService(service_no, param_no, payReqMap)) {
			errorAfterProcess(response);
			return payRspMap;
		}

		// VIP充值
		if (SystemConstant.SERVICE_NO_100002.equals(service_no)) {
			payReqMap.put("trans_id", SystemConstant.PAY_RECHARGE_CODE);// 交易码
			payReqMap.put("trans_nm", SystemConstant.PAY_RECHARGE_MSG);// 交易名称
		}

		try {			
			// 检查会员信息是否存在(判断会员等级)
			if (!mainBusinessService.checkCustomerInfo(payReqMap)) {
				errorAfterProcess(response);
				return payRspMap;
			}
			
			if (!appLogTransJnlsPayService.save(payReqMap, null)
					&& !StringUtils.isEmpty(payReqMap.get(SystemConstant.RSP_CODE))) {
				errorAfterProcess(response);
				return payRspMap;
			}
			payReqMap.put("trans_at", String.valueOf(MathUtils.div(Double.valueOf(payReqMap.get("trans_at")), 100)));
			
			if (!appPayDao.getActUuid(payReqMap.get("act_out_no"))) {
				String name = payReqMap.get("name");
				String phoneNo = payReqMap.get("phone");
				String certificate_no = payReqMap.get("certificate_no");
				Map<String, String> paramMap = new HashMap<String, String>();
				paramMap.put(SystemConstant.SESSION_LOGIN_USER, login_id);
				/*查询会员所属app机构，获取机构uuid*/
				App_Custom_InfEntity customer = customerInfoService.getCustomByNo(custom_no);
				Map<String, Object> resultMap = iFourElementService.queryFourElement(new FourElements(customer.getIns_uuid(), name, phoneNo, certificate_no, act_out_no), paramMap);
				if (!SystemConstant.RSPCODE_SUCCESS.equals(resultMap.get(SystemConstant.RSP_CODE))) {
					appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300028.substring(4, 6),
							String.valueOf(resultMap.get(SystemConstant.RSP_MSG)));
					errorAfterProcess(response);
					return payRspMap;
				}
				
				boolean result = mainBusinessService.saveCardInfo(payReqMap,null);
				if(!result){
					appLogTransJnlsPayService.update(payReqMap, payReqMap.get(SystemConstant.RSP_CODE).substring(4, 6),
							String.valueOf(getRspCodeListMap().get(payReqMap.get(SystemConstant.RSP_CODE))));
					errorAfterProcess(response);
					return payRspMap;
				}
			}else{
				mainBusinessService.getActOutNo(payReqMap);
			}
			
			// 根据卡号查询卡bin及卡性质(获取卡bin信息)
			if (!mainBusinessService.getCardBinAttrInfo(payReqMap) && !StringUtils.isEmpty(payReqMap.get(SystemConstant.RSP_CODE))) {
				appLogTransJnlsPayService.update(payReqMap, payReqMap.get(SystemConstant.RSP_CODE).substring(4, 6),
						String.valueOf(getRspCodeListMap().get(payReqMap.get(SystemConstant.RSP_CODE))));
				errorAfterProcess(response);
				return payRspMap;
			}

			// 根据会员编号查询收款账号信息(收款账户及开户行)
			if (!mainBusinessService.getActInCardInfo(payReqMap)) {
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300054.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300054)));
				errorAfterProcess(response);
				return payRspMap;
			}
			
			// 会员状态合法性检查
			if (!mainBusinessService.checkCustomerState(payReqMap)
					&& !StringUtils.isEmpty(payReqMap.get(SystemConstant.RSP_CODE))) {
				appLogTransJnlsPayService.update(payReqMap, payReqMap.get(SystemConstant.RSP_CODE).substring(4, 6),
						String.valueOf(getRspCodeListMap().get(payReqMap.get(SystemConstant.RSP_CODE))));
				errorAfterProcess(response);
				return payRspMap;
			}
			
			// 机构状态合法性检查
			if (!mainBusinessService.checkInsState(payReqMap)
					&& !StringUtils.isEmpty(payReqMap.get(SystemConstant.RSP_CODE))) {
				appLogTransJnlsPayService.update(payReqMap, payReqMap.get(SystemConstant.RSP_CODE).substring(4, 6),
						String.valueOf(getRspCodeListMap().get(payReqMap.get(SystemConstant.RSP_CODE))));
				errorAfterProcess(response);
				return payRspMap;
			}		

			// 会员、机构限额合法性校验，校验不通过直接更新流水状态
			if (!mainBusinessService.validateParamLimit(payReqMap)) {
				String desc = String.format(String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300007)),payReqMap.get(SystemConstant.RSP_MSG));
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300007.substring(4, 6),
						desc);
				errorAfterProcess(response);
				return payRspMap;
			}

			// 交易受理类型
			if (!mainBusinessService.checkTranType(payReqMap)) {
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300008.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300008)));
				errorAfterProcess(response);
				return payRspMap;
			}
			
			// 根据订单相关参数获取交易路由服务，判断最终走哪些通道分组。
			log.info(
					"路由调用的参数[login_id={},act_out_no={},custom_no={},custom_level={},ins_uuid={},area_uuid={},service_uuid={},"
					+ "service_param_uuid={},card_ins_cd={},crd_nature={},trans_at={},service_ins_uuid={},card_bin={}]",
					login_id, payReqMap.get("act_out_no"), payReqMap.get("custom_no"), payReqMap.get("custom_level"),
					payReqMap.get("ins_uuid"), payReqMap.get("area_uuid"), payReqMap.get("service_uuid"),
					payReqMap.get("service_param_uuid"), payReqMap.get("card_ins_cd"), payReqMap.get("crd_nature"),
					Double.valueOf(payReqMap.get("trans_at")), payReqMap.get("service_ins_uuid"),
					payReqMap.get("card_bin"),payReqMap.get("loc_trace_no"));
			Map<String, String> routeMap = transRouteService.getTransRouteInfo(login_id, payReqMap.get("act_out_no"),
					payReqMap.get("custom_no"), payReqMap.get("custom_level"), payReqMap.get("ins_uuid"),
					payReqMap.get("area_uuid"), payReqMap.get("service_uuid"), payReqMap.get("service_param_uuid"),
					payReqMap.get("card_ins_cd"), payReqMap.get("crd_nature"),
					Double.valueOf(payReqMap.get("trans_at")), payReqMap.get("service_ins_uuid"),
					payReqMap.get("card_bin"),payReqMap.get("loc_trace_no"));
			IQuickPayService payService = getChannelService(routeMap);// 获取渠道服务		
			if ("1".equals(routeMap.get("resultNum"))) {
				if (payService == null) {
					log.error("[{}]交易流水路由返回的渠道编号无对应的渠道服务,路由信息=[{}]！", payReqMap.get("loc_trace_no"), routeMap);
					appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300033.substring(4, 6),
							String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300033)));
					errorAfterProcess(response);
					return payRspMap;
				}
				if (!appLogTransJnlsPayService.update(payReqMap, routeMap)) {
					log.error("[{}]交易流水更新发生异常[routeMap={},payReqMap={}]！", payReqMap.get("loc_trace_no"), routeMap,
							payReqMap);
					errorAfterProcess(response);
					return payRspMap;
				}
			} else {
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300026.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300026)));
				errorAfterProcess(response);
				return payRspMap;
			}

			Map<String, String> dataMap = CommonService.getStandardData(payReqMap);// 报文标准化处理
			log.info("系统请求报文转标准化渠道请求报文:[{}]",dataMap);
			Map rspMap = payService.payOrder(routeMap.get("trf_mchnt_cd"), order_no, payReqMap.get("trans_at"),
					payReqMap.get("phone"), payReqMap.get("certificate_no"), payReqMap.get("acct_nm"),
					payReqMap.get("act_out_no"), null, dataMap);
			// 渠道异常
			if (StringUtils.isEmpty(rspMap)) {
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300035.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300035)));
				errorAfterProcess(response);
				return payRspMap;
			}
			payRspMap.put("req_trace_no", trace_no);
			payRspMap.put("order_no", order_no);
			String rspCode = CommonService.encapResultCode(String.valueOf(rspMap.get("rspcode")));
			if (!SystemConstant.RSPCODE_SUCCESS.equals(rspCode)) {
				String rspMsg = null;
				if (!StringUtils.isEmpty(rspMap.get("rspmsg"))) {
					rspMsg = String.valueOf(rspMap.get("rspmsg"));
				}
				appLogTransJnlsPayService.update(payReqMap, String.valueOf(rspMap.get("rspcode")), rspMsg);
				payRspMap.put("trs_stat", SystemConstant.RSPCODE_ERROR);
				errorAfterProcess(response);
				return payRspMap;
			} else {
				log.info("支付成功异步通知管理系统 loc_trace_no=" + payReqMap.get("loc_trace_no"));
				mainBusinessService.asyncNotifyMpos(payReqMap.get("loc_trace_no"));
				appLogTransJnlsPayService.updateTraceNo(payReqMap.get("loc_trace_no"),String.valueOf(rspMap.get("loc_dt")),String.valueOf(rspMap.get("loc_ts")));
				payRspMap = new HashMap<String, String>();
				payRspMap.put("channel_type", String.valueOf(rspMap.get("channel_type")));
				payRspMap.put("stl_dt", String.valueOf(rspMap.get("loc_dt")));
				payRspMap.put("pay_info", String.valueOf(rspMap.get("sourceData")));
				payRspMap.put("action_url", String.valueOf(rspMap.get("action")));
				payRspMap.put("trs_stat", SystemConstant.RSPCODE_SUCCESS);
				payRspMap.put("channel_trace_no", payReqMap.get("loc_trace_no"));
				payRspMap.put("verify_code_type", String.valueOf(rspMap.get("verify_code_type")));
				response.setRsp_code(SystemConstant.RSPCODE_SUCCESS);
				response.setRsp_msg(SystemConstant.RSPCODE_SUCCESS_MSG);
				return payRspMap;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 
	 * 接口: 外接发送短信验证码
	 * @param request
	 * @param response
	 * @param gpsEntity
	 * @param random_number
	 * @param custom_no
	 * @param loc_trace_no
	 * @param phone
	 * @param login_id
	 * @param serviceData
	 * @return
	 * @since 2017年12月14日
	 */
	public Map sendGateVerifyCode(HttpServletRequest request, RequestDTO requestDto, Map<String, String> bizContent, ResponseDTO response) {
		Map<String, String> payReqMap = new HashMap<String, String>();// 请求标准化报文
		Map<String, String> payRspMap = null;// 响应报文
		
		/*String version = requestDto.getVersion();
		String instCode = requestDto.getInstCode();
		String trace_no = requestDto.getRequestId();
		String tran_dt_tm = bizContent.get("tran_dt_tm");*/
		String loc_trace_no = bizContent.get("channel_trace_no"); 
		String custom_no = bizContent.get("mchnt_cd"); 
		/*String crd_exp = bizContent.get("crd_exp"); 
		String phone = bizContent.get("phone"); 
		String cvv2 = bizContent.get("cvv2"); */
		
		try {
			payReqMap.put("custom_no", custom_no);// 客户编号
			payReqMap.put("loc_trace_no", loc_trace_no);// 流水号
			/*payReqMap.put("tran_dt_tm", tran_dt_tm);// 流水号
			payReqMap.put("crd_exp", crd_exp);
			payReqMap.put("phone", phone);
			payReqMap.put("cvv2", cvv2);*/

			// 检查会员信息是否存在(判断会员等级)
			if (!mainBusinessService.checkCustomerInfo(payReqMap)) {
				errorAfterProcess(response);
				return payRspMap;
			}
	
			Map<String, String> tranInfo = appLogTransJnlsPayService.getTranInfo(loc_trace_no);
			payReqMap.put("loc_dt", tranInfo.get("loc_dt"));
			payReqMap.put("loc_ts", tranInfo.get("loc_ts"));
			payReqMap.put("trf_mchnt_cd", tranInfo.get("trf_mchnt_cd"));
			payReqMap.put("act_out_no", tranInfo.get("act_out_no"));
			mainBusinessService.getActOutNo(payReqMap);
			Map<String, String> dataMap = CommonService.getStandardData(payReqMap);// 报文标准化处理
			IQuickPayService payService = CommonService.getPayServices().get(tranInfo.get("trf_channel_id"));
			if (StringUtils.isEmpty(payService)) {
				log.error("[{}]交易流水路由返回的渠道编号无对应的[{}]渠道服务", payReqMap.get("loc_trace_no"), tranInfo.get("trf_channel_id"));
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300033.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300033)));
				errorAfterProcess(response);
				return payRspMap;
			}
			Map rspMap = payService.sendVerifyCode(dataMap);
			// 渠道异常
			if (StringUtils.isEmpty(rspMap)) {
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300035.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300035)));
				errorAfterProcess(response);
				return payRspMap;
			}
			String rspCode = CommonService.encapResultCode(String.valueOf(rspMap.get("rspcode")));
			if (!SystemConstant.RSPCODE_SUCCESS.equals(rspCode)) {
				String rspMsg = null;
				if (!StringUtils.isEmpty(rspMap.get("rspmsg"))) {
					rspMsg = String.valueOf(rspMap.get("rspmsg"));
				}
				appLogTransJnlsPayService.update(payReqMap, String.valueOf(rspMap.get("rspcode")), rspMsg);
				errorAfterProcess(response);
				return payRspMap;
			} else {
				appLogTransJnlsPayService.updateTraceNo(payReqMap.get("loc_trace_no"),String.valueOf(rspMap.get("loc_dt")),String.valueOf(rspMap.get("loc_ts")));
				payRspMap = new HashMap<String, String>();
				payRspMap.put("mchnt_cd", custom_no);
				payRspMap.put("channel_trace_no", loc_trace_no);// 本地流水号
				payRspMap.put("phone", payReqMap.get("phone"));
				response.setRsp_code(SystemConstant.RSPCODE_SUCCESS);
				response.setRsp_msg(SystemConstant.RSPCODE_SUCCESS_MSG);
				return payRspMap;
			}

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 
	 * 接口: 外接手机验证码接口
	 * @param request
	 * @param response
	 * @param gpsEntity
	 * @param random_number
	 * @param custom_no
	 * @param loc_trace_no
	 * @param phone
	 * @param verify_code
	 * @param pay_pass_word
	 * @param login_id
	 * @param serviceData
	 * @return
	 * @since 2017年12月14日
	 */
	public Map gateVerifyCode(HttpServletRequest request, RequestDTO requestDto, Map<String, String> bizContent, ResponseDTO response) {
		Map<String, String> payReqMap = new HashMap<String, String>();// 请求标准化报文
		Map<String, String> payRspMap = null;// 响应报文
		try {
			/*String version = requestDto.getVersion();
			String instCode = requestDto.getInstCode();
			String trace_no = requestDto.getRequestId();*/
			String loc_trace_no = bizContent.get("channel_trace_no"); 
			String verify_code = bizContent.get("verify_code");
			String custom_no = bizContent.get("mchnt_cd"); 
			
			payReqMap.put("custom_no", custom_no);// 客户编号
			payReqMap.put("loc_trace_no", loc_trace_no);// 流水号
			payReqMap.put("verify_code", verify_code);// 验证码密文

			// 检查会员信息是否存在(判断会员等级)
			if (!mainBusinessService.checkCustomerInfo(payReqMap)) {
				errorAfterProcess(response);
				return payRspMap;
			}
	
			Map<String, String> tranInfo = appLogTransJnlsPayService.getTranInfo(loc_trace_no);
			payReqMap.put("loc_dt", tranInfo.get("loc_dt"));
			payReqMap.put("loc_ts", tranInfo.get("loc_ts"));
			payReqMap.put("trf_mchnt_cd", tranInfo.get("trf_mchnt_cd"));
			payReqMap.put("act_out_no", tranInfo.get("act_out_no"));
			mainBusinessService.getActOutNo(payReqMap);
			Map<String, String> dataMap = CommonService.getStandardData(payReqMap);// 报文标准化处理
			IQuickPayService payService = CommonService.getPayServices().get(tranInfo.get("trf_channel_id"));
			if (StringUtils.isEmpty(payService)) {
				log.error("[{}]交易流水路由返回的渠道编号无对应的[{}]渠道服务", payReqMap.get("loc_trace_no"), tranInfo.get("trf_channel_id"));
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300033.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300033)));
				errorAfterProcess(response);
				return payRspMap;
			}
			Map rspMap = payService.verifyCode(dataMap);
			// 渠道异常
			if (StringUtils.isEmpty(rspMap)) {
				appLogTransJnlsPayService.update(payReqMap, RspCodeConstant.RSPCODE_300035.substring(4, 6),
						String.valueOf(getRspCodeListMap().get(RspCodeConstant.RSPCODE_300035)));
				errorAfterProcess(response);
				return payRspMap;
			}
			String rspCode = CommonService.encapResultCode(String.valueOf(rspMap.get("rspcode")));
			if (!SystemConstant.RSPCODE_SUCCESS.equals(rspCode)) {
				String rspMsg = null;
				if (!StringUtils.isEmpty(rspMap.get("rspmsg"))) {
					rspMsg = String.valueOf(rspMap.get("rspmsg"));
				}
				appLogTransJnlsPayService.update(payReqMap, String.valueOf(rspMap.get("rspcode")), rspMsg);
				errorAfterProcess(response);
				return payRspMap;
			} else {
				appLogTransJnlsPayService.updateTraceNo(payReqMap.get("loc_trace_no"),String.valueOf(rspMap.get("loc_dt")),String.valueOf(rspMap.get("loc_ts")));
				payRspMap = new HashMap<String, String>();
				payRspMap.put("mchnt_cd", custom_no);
				payRspMap.put("channel_trace_no", loc_trace_no);// 本地流水号
				payRspMap.put("phone", payReqMap.get("phone"));
				payRspMap.put("verify_code", verify_code);
				response.setRsp_code(SystemConstant.RSPCODE_SUCCESS);
				response.setRsp_msg(SystemConstant.RSPCODE_SUCCESS_MSG);
				return payRspMap;
			}

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 外接订单查询
	 * @param request 请求对象
	 * @param requestDto  请求实体
	 * @param bizContent 业务对象
	 * @param response 响应对象
	 * @return
	 */
	public Map queryGateService(HttpServletRequest request, RequestDTO requestDto, Map<String, String> bizContent, ResponseDTO response){
		//log.info("订单查询order_no=" + order_no + "请求");
		Map<String, String> payReqMap = new HashMap<String, String>();// 请求标准化报文
		Map<String, String> payRspMap = new HashMap<String, String>();// 响应报文
		
		/*String version = requestDto.getVersion();
		String instCode = requestDto.getInstCode();
		String trace_no = requestDto.getRequestId();*/
		String order_no = bizContent.get("order_no");
		
		// 组装标准化处理报文
		payReqMap.put("inte_flag", "gate");// 客户编号
		/*payReqMap.put("version", version);// 客户编号
		payReqMap.put("instCode", instCode);// 客户编号
		payReqMap.put("trace_no", trace_no);// 客户编号
*/		payReqMap.put("order_no", order_no);// 订单号
		
		try {						
			payReqMap.put("order_no", order_no);// 订单号
			/*payReqMap.put("trace_no", trace_no);// 订单号
*/
			
			payRspMap.put("order_no", order_no);
			// 查询订单结果
			Map<String, String> transMap = appLogTransJnlsPayService.getTransJnlsByNo(payReqMap.get("order_no"),
					null, null,"gate");
			if(transMap == null || transMap.isEmpty()){
				payRspMap.put("trs_stat", "96");
				response.setRsp_code(SystemConstant.RSPCODE_SUCCESS);
				response.setRsp_msg(SystemConstant.RSPCODE_SUCCESS_MSG);
				return payRspMap;
			}

			// TODO 重新组装标准化响应消息
			payRspMap.put("channel_trace_no", transMap.get("loc_trace_no"));
			payRspMap.put("trans_at", String.valueOf(transMap.get("gate_trans_at")));
			payRspMap.put("midfee", String.valueOf(transMap.get("gate_midfee")));
			payRspMap.put("costfee", String.valueOf(transMap.get("costfee")));
			payRspMap.put("mchnt_cd", transMap.get("custom_no"));
			payRspMap.put("pay_success_time", (StringUtils.isEmpty(transMap.get("complete_dt")) ? "" : transMap.get("complete_dt"))+ (StringUtils.isEmpty(transMap.get("complete_ts")) ? "" : transMap.get("complete_ts")));
			payRspMap.put("trs_stat", transMap.get("trs_stat"));
			payRspMap.put("stl_st",  transMap.get("stlst"));
			payRspMap.put("stl_dt", transMap.get("stl_dt"));
			payRspMap.put("stl_amt", transMap.get("gate_stl_amt"));
			payRspMap.put("stl_time", (StringUtils.isEmpty(transMap.get("stl_dt")) ? "" : transMap.get("stl_dt"))+ (StringUtils.isEmpty(transMap.get("pay_ts")) ? "" : transMap.get("pay_ts")));
			response.setRsp_code(SystemConstant.RSPCODE_SUCCESS);
			response.setRsp_msg(SystemConstant.RSPCODE_SUCCESS_MSG);
			return payRspMap;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}		
	}
	
	private void errorAfterProcess(ResponseDTO response) {
		response.setRsp_code(SystemConstant.RSPCODE_ERROR);
		response.setRsp_msg(SystemConstant.RSPCODE_ERROR_MSG);
	}
}
